package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewpager.widget.ViewPager;

import com.hjq.base.FragmentPagerAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.RoomGalleryBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.manager.ActivityManager;
import com.qiantu.phone.ui.fragment.RoomGalleryFragment;
import com.qiantu.phone.ui.fragment.RoomListFragment;
import com.qiantu.phone.widget.ViewPagerScrollIndicator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class RoomGalleryActivity extends AppActivity {
    private ViewPagerScrollIndicator tabBar;
    private ViewPager viewPager;
    private RoomBean roomBean;
    private String selectPicUrl;
    @Override
    protected int getLayoutId() {
        return R.layout.room_gallery_activity;
    }

    @Override
    protected void initView() {
        tabBar = findViewById(R.id.tab_bar);
        viewPager = findViewById(R.id.view_pager);
        getTitleBar().getRightView().setEnabled(true);
    }

    @Override
    public void onRightClick(View view){
        LLHttpManager.saveRoomPic(this,roomBean.getRoomSerialNo(),selectPicUrl,new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog("请稍后");
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                roomBean.setPicUrl(selectPicUrl);
                LLRoomDBManager.getInstance(getContext()).put(roomBean);
                EventBus.getDefault().post(new RefreshFloorRoomEvent());
                finish();
                ActivityManager.getInstance().finishActivity(RoomSettingActivity.class);
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(e.getMessage());
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    @Override
    protected void initData() {
        roomBean = LLRoomDBManager.getInstance(getContext()).getRoomByRoomSerialNo(getIntent().getStringExtra("roomSerialNo"));
        selectPicUrl = roomBean.getPicUrl();
        LLHttpManager.getDefaultGallery(this, new HttpCallback<HttpData<List<RoomGalleryBean>>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog("请稍后");
            }

            @Override
            public void onSucceed(HttpData<List<RoomGalleryBean>> result) {
                setGalleryData(result.getData());
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(e.getMessage());
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    public void setSelectPicUrl(String selectPicUrl){
        this.selectPicUrl = selectPicUrl;
    }

    public void setGalleryData(List<RoomGalleryBean> data){
        List<String> typeTitle = new ArrayList<>();
        FragmentPagerAdapter<Fragment> mPagerAdapter = new FragmentPagerAdapter<>(this);
        int index = 0;
        for(int i = 0;i < data.size();i++){
            RoomGalleryBean bean = data.get(i);
            typeTitle.add(bean.getRoomTypeName());
            RoomGalleryFragment fragment = RoomGalleryFragment.newInstance(bean.getImages(),selectPicUrl);
            mPagerAdapter.addFragment(fragment);
            if(bean.getRoomTypeName().equals(getString("roomTypeName"))){
                index = i;
            }
        }
        viewPager.setAdapter(mPagerAdapter);
        tabBar.setupViewpager(viewPager);
        tabBar.setTitleList(typeTitle);
        tabBar.selectTab(index);
        viewPager.setCurrentItem(index);
    }
}
